package com.bianmaba.spring.data.jpa.domain.specification.condition.predicate.generator;


import com.bianmaba.spring.data.jpa.domain.specification.condition.param.Param;
import com.bianmaba.spring.data.jpa.domain.specification.condition.param.converter.ConverterManager;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.lang.reflect.Method;

/**
 * 小于
 */
public class NotinPredicateGenerator extends AbstractPredicateGenerator {
    protected Method getCreateMethod(CriteriaBuilder builder) {
        try {
            Method method = builder.getClass().getMethod("in", Expression.class);
            return method;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Predicate toPredicate(CriteriaBuilder builder, Path path, Param param) {
        Class clazz = path.getJavaType();
        Object[] values = ConverterManager.get(clazz).transform(param.getValue());
        Method method = getCreateMethod(builder);
        if (method == null) {
            return null;
        }
        return builder.not(path.in(values));
    }
}